package com.nmf.pc.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CORSFilter implements Filter {
	private final static Logger log = LoggerFactory.getLogger(CORSFilter.class);

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		if(((HttpServletRequest) servletRequest).getServletPath().contains("css") || ((HttpServletRequest) servletRequest).getServletPath().contains("htm")
				|| ((HttpServletRequest) servletRequest).getServletPath().contains("js")
				) {
			filterChain.doFilter(servletRequest, servletResponse);
		}else {
			try {
				HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
				httpResponse.addHeader("Access-Control-Allow-Origin", "*");
				filterChain.doFilter(servletRequest, servletResponse);
			} catch (Exception e) {
				log.error("", e);
			}
		}
	}

	@Override
	public void destroy() {

	}
}